package com.tanxi521.tnlevel;

import com.tanxi521.tnlevel.command.TnlevelCommands;
import com.tanxi521.tnlevel.dao.ConnectionPool;
import com.tanxi521.tnlevel.dao.SQLManager;
import com.tanxi521.tnlevel.listener.TnLevelListener;
import com.tanxi521.tnlevel.placeholder.TnLevelPlaceHolder;
import com.tanxi521.tnlevel.utils.ConfigReader;
import com.tanxi521.tnlevel.utils.LevelCounter;
import lombok.Getter;
import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.Objects;


public final class TnLevel extends JavaPlugin {

    @Getter
    public static TnLevel instance;

    @Override
    public void onLoad() {
        instance = this;
        saveDefaultConfig();

    }

    @Override
    public void onEnable() {
        getLogger().info("§e北境等级系统启动中...");
        getLogger().info("§e版本号: " + getDescription().getVersion());
        getLogger().info("§e作者: " + getDescription().getAuthors());
        getLogger().info("§e联系方式: QQ 1254806967");
        LevelCounter.initCounter();
        if (ConfigReader.getMysqlUse()) ConnectionPool.initPoolMysql();
        else ConnectionPool.initPoolSQLITE();
        SQLManager.createTable();
        Objects.requireNonNull(Bukkit.getPluginCommand("tnlevel")).setExecutor(new TnlevelCommands());
        if (PlaceholderAPI.isRegistered("tn")) PlaceholderAPI.unregisterPlaceholderHook("tn");
        boolean register = new TnLevelPlaceHolder().register();

        Bukkit.getPluginManager().registerEvents(new TnLevelListener(), this);
        if (register) {
            getLogger().info("PlaceHolderAPI注册成功");
        }
        getLogger().info("§e北境等级系统启动完毕");
    }

    @Override
    public void onDisable() {
        boolean tn = PlaceholderAPI.unregisterPlaceholderHook("tn");
        if (tn) {
            getLogger().info("PlaceHolderAPI已注销");
        }
        ConnectionPool.close(ConnectionPool.pool);
    }
}
